package tvp.ltdd.fake_call_sms.activity;

import java.util.Calendar;

import tvp.ltdd.fake_call_sms.R;
import tvp.ltdd.fake_call_sms.receiver.MessageReceiver;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class FragmentQuickSMS extends Fragment implements OnClickListener,
		OnTouchListener {
	Button btn15s, btn30s, btn1m, btnContact, btnPlus, btnSave;
	EditText etName, etNumber, etMessage;
	private int time = 0;
	private int hour;
	private int minute;
	private int current_hour;
	private int current_minute;
	private final Calendar c = Calendar.getInstance();
	private final int PICK = 1;
	private final int TIME_DIALOG = 3;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.qck_sms, container, false);
		btn15s = (Button) rootView.findViewById(R.id.btn15s);
		btn30s = (Button) rootView.findViewById(R.id.btn30s);
		btn1m = (Button) rootView.findViewById(R.id.btn1m);
		btnPlus = (Button) rootView.findViewById(R.id.btnPlus);
		btnSave = (Button) rootView.findViewById(R.id.btnSave);
		btnContact = (Button) rootView.findViewById(R.id.btnContact);
		etName = (EditText) rootView.findViewById(R.id.etName);
		etNumber = (EditText) rootView.findViewById(R.id.etNumber);
		etMessage = (EditText) rootView.findViewById(R.id.etMessage);

		btnContact = (Button) rootView.findViewById(R.id.btnContact);

		btn15s.setOnTouchListener(this);
		btn30s.setOnTouchListener(this);
		btn1m.setOnTouchListener(this);
		btnPlus.setOnClickListener(this);
		btnContact.setOnClickListener(this);
		btnSave.setOnClickListener(this);

		return rootView;
	}

	@Override
	public void onActivityResult(int reqCode, int resultCode, Intent data) {
		super.onActivityResult(reqCode, resultCode, data);
		switch (reqCode) {
		case (PICK):
			if (resultCode == Activity.RESULT_OK) {
				Uri contactData = data.getData();
				Cursor c = getActivity().getContentResolver().query(
						contactData, null, null, null, null);
				if (c.moveToFirst()) {
					String id = c
							.getString(c
									.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

					String hasPhone = c
							.getString(c
									.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

					if (hasPhone.equalsIgnoreCase("1")) {
						Cursor phones = getActivity()
								.getContentResolver()
								.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
										null,
										ContactsContract.CommonDataKinds.Phone.CONTACT_ID
												+ " = " + id, null, null);
						phones.moveToFirst();
						String cNumber = phones
								.getString(phones
										.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
						String nameContact = c
								.getString(c
										.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

						etName.setText(nameContact);
						etNumber.setText(cNumber);

					}
				}
			}
			break;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnContact:
			Intent intent = new Intent(Intent.ACTION_PICK,
					ContactsContract.Contacts.CONTENT_URI);
			startActivityForResult(intent, PICK);
			break;
		case R.id.btnSave:
			if (time != 0) {
				if (etNumber.getText().toString().trim().length() != 0) {
					if (etMessage.getText().toString().trim().length() != 0) {
						Log.v("message object", "name = " + etName.getText()
								+ " number = " + etNumber.getText()
								+ "message=" + etMessage.getText());
						MessageReceiver mr = new MessageReceiver();
						mr.setMessage(getActivity(), etName.getText()
								.toString(), etNumber.getText().toString(),
								etMessage.getText().toString(), 1, time * 1000);
						Toast.makeText(getActivity(),
								"New message has been placed",
								Toast.LENGTH_LONG).show();
						getActivity().finish();
					} else
						Toast.makeText(getActivity(), "Please input message!",
								Toast.LENGTH_SHORT).show();
				} else
					Toast.makeText(getActivity(), "Please input number!",
							Toast.LENGTH_SHORT).show();
			} else
				Toast.makeText(getActivity(), "Please set time!",
						Toast.LENGTH_SHORT).show();

			break;
		case R.id.btnPlus:
			onCreateDialog(TIME_DIALOG).show();
			break;
		}
		Log.v("time on click", "" + time);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.btn15s:
			btn15s.setPressed(true);
			btn30s.setPressed(false);
			btn1m.setPressed(false);
			btnPlus.setPressed(false);
			time = 15;
			break;
		case R.id.btn30s:
			btn15s.setPressed(false);
			btn30s.setPressed(true);
			btn1m.setPressed(false);
			btnPlus.setPressed(false);
			time = 30;
			break;
		case R.id.btn1m:
			btn15s.setPressed(false);
			btn30s.setPressed(false);
			btn1m.setPressed(true);
			btnPlus.setPressed(false);
			time = 60;
			break;
		}
		return true;
	}

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case TIME_DIALOG:

			hour = c.get(Calendar.HOUR_OF_DAY);
			minute = c.get(Calendar.MINUTE);
			Log.v("time", "hour = " + hour + " minute = " + minute);
			return new TimePickerDialog(getActivity(), timePickerListener,
					hour, minute + 1, true);

		}
		return null;
	}

	private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int selectedHour,
				int selectedMinute) {
			hour = selectedHour;
			minute = selectedMinute;

			// set current time into textview
			btnPlus.setText(new StringBuilder().append(pad(hour)).append(":")
					.append(pad(minute)));
			current_hour = c.get(Calendar.HOUR_OF_DAY);
			current_minute = c.get(Calendar.MINUTE);
			Log.v("time", "current = " + current_hour + ":" + current_minute
					+ " selected = " + hour + ":" + minute);
			if (current_hour == hour) {
				time = (minute - current_minute) * 60;
			} else {
				time = (((hour - current_hour) * 60 + minute) - current_minute) * 60;
			}
		}
	};

	private static String pad(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}
}
